//define calculator window
function defineCalculatorWindow(){
	hf.widget.calculatorWindow = Ext.extend(Ext.Window, {
		constructor: function(config) {
			Ext.apply(this, {
				title: labels['calculator.title'],
				layout:'fit',
				width:200,
				height:200,
				shadow: new Ext.Shadow({mode: 'drop'}),
				plain: true,
				modal: false,
				draggable: true,
				items: [{
					
					xtype: 'container',
					layout: 'anchor',
					items: [
						{xtype: 'container', anchor: '100% 20%', layout: {type: 'hbox', align: 'stretch'},
							items: [
								{xtype: 'textfield',  ref: '../../display', readOnly: true, anchor: '100% 20%', flex: 1, value: '0', style : {fontWeight: 'bold', textAlign: 'right'}}		
							]
						},
						{xtype: 'container',  anchor: '100% 20%', layout: {type: 'hbox', align: 'stretch'},
							items: [
								{xtype: 'form-button', text: '7', value: 7, buttonEvent: 'numPressedAction', ref: '../../button7', flex: 1},
								{xtype: 'form-button', text: '8', value: 8, buttonEvent: 'numPressedAction', ref: '../../button8', flex: 1},
								{xtype: 'form-button', text: '9', value: 9, buttonEvent: 'numPressedAction', ref: '../../button9', flex: 1},
								{xtype: 'form-button', text: 'CE', value: 'CE', buttonEvent: 'clearEntryAction', ref: '../../buttonCleanEntry', flex: 1},
								{xtype: 'form-button', text: 'C', value: 'C', buttonEvent: 'clearAction', ref: '../../buttonClean', flex: 1}
							]
						},
						{xtype: 'container',  anchor: '100% 20%', layout: {type: 'hbox', align: 'stretch'},
							items: [
								{xtype: 'form-button', text: '4', value: 4, buttonEvent: 'numPressedAction', ref: '../../button4', flex: 1},
								{xtype: 'form-button', text: '5', value: 5, buttonEvent: 'numPressedAction', ref: '../../button5', flex: 1},
								{xtype: 'form-button', text: '6', value: 6, buttonEvent: 'numPressedAction', ref: '../../button6', flex: 1},
								{xtype: 'form-button', text: '+', value: '+', buttonEvent: 'operationAction', ref: '../../buttonSum', flex: 1},
								{xtype: 'form-button', text: '-', value: '-', buttonEvent: 'operationAction', ref: '../../buttonMinus', flex: 1}
							]
						},
						{xtype: 'container',  anchor: '100% 20%', layout: {type: 'hbox', align: 'stretch'},
							items: [
								{xtype: 'form-button', text: '1', value: 1, buttonEvent: 'numPressedAction', ref: '../../button1', flex: 1},
								{xtype: 'form-button', text: '2', value: 2, buttonEvent: 'numPressedAction', ref: '../../button2', flex: 1},
								{xtype: 'form-button', text: '3', value: 3, buttonEvent: 'numPressedAction', ref: '../../button3', flex: 1},
								{xtype: 'form-button', text: '*', value: '*', buttonEvent: 'operationAction', ref: '../../buttonMultiply', flex: 1},
								{xtype: 'form-button', text: '/', value: '/', buttonEvent: 'operationAction', ref: '../../buttonDivide', flex: 1}
							]
						},
						{xtype: 'container',  anchor: '100% 20%', layout: {type: 'hbox', align: 'stretch'},
							items: [
								{xtype: 'form-button', text: '.', value: '.', buttonEvent: 'decimalAction', ref: '../../buttonPoint', flex: 1},
								{xtype: 'form-button', text: '0', value: '0', buttonEvent: 'numPressedAction', ref: '../../button0', flex: 1},
								{xtype: 'form-button', text: '+/-', value: '+/-', buttonEvent: 'negativeAction', ref: '../../buttonSign', flex: 1},
								{xtype: 'form-button', text: '%', value: '%', buttonEvent: 'percentAction', ref: '../../buttonPercent', flex: 1},
								{xtype: 'form-button', text: '=', value: '=', buttonEvent: 'operationAction', ref: '../../buttonEquate', flex: 1}
							]
						}
					]
				}],
				
				currents: 0,
				flagNewNum: false,
				pendingOp: ''
			});
			hf.widget.calculatorWindow.superclass.constructor.apply(this, arguments);
			
			this.on('numPressedAction', function(button, event){this.numPressedAction(button.value);});
			this.on('operationAction', function(button, event){this.operationAction(button.value);});
			this.on('clearAction', function(button, event){this.clearAction();});
			this.on('clearEntryAction', function(button, event){this.clearEntryAction();});
			this.on('negativeAction', function(button, event){this.negativeAction();});
			this.on('percentAction', function(button, event){this.percentAction();});
			this.on('decimalAction', function(button, event){this.decimalAction();});
			
			this.relayEvents(this.button7, ['numPressedAction']);
			this.relayEvents(this.button8, ['numPressedAction']);
			this.relayEvents(this.button9, ['numPressedAction']);
			this.relayEvents(this.buttonCleanEntry, ['clearEntryAction']);
			this.relayEvents(this.buttonClean, ['clearAction']);
		
			this.relayEvents(this.button4, ['numPressedAction']);
			this.relayEvents(this.button5, ['numPressedAction']);
			this.relayEvents(this.button6, ['numPressedAction']);
			this.relayEvents(this.buttonSum, ['operationAction']);
			this.relayEvents(this.buttonMinus, ['operationAction']);
		
			this.relayEvents(this.button1, ['numPressedAction']);
			this.relayEvents(this.button2, ['numPressedAction']);
			this.relayEvents(this.button3, ['numPressedAction']);
			this.relayEvents(this.buttonMultiply, ['operationAction']);
			this.relayEvents(this.buttonDivide, ['operationAction']);
		
			this.relayEvents(this.buttonPoint, ['decimalAction']);
			this.relayEvents(this.button0, ['numPressedAction']);
			this.relayEvents(this.buttonSign, ['negativeAction']);
			this.relayEvents(this.buttonPercent, ['percentAction']);
			this.relayEvents(this.buttonEquate, ['operationAction']);
		},
		
		numPressedAction: function(value){
			if(this.flagNewNum){
				this.setDisplayValue(value);
				this.flagNewNum = false;
			}else{
				if(this.getDisplayValue() == '0'){
					this.setDisplayValue(value);
				}else{
					this.setDisplayValue(this.getDisplayValue() + value);
				}
			}
		},
		
		operationAction: function(value){
			var currentValue = this.getDisplayValue();
			if(this.flagNewNum && this.pendingOp != '='){
				this.setDisplayValue(this.currents);
			}else{
				this.flagNewNum = true;
				if('+' == this.pendingOp){
					this.currents += parseFloat(currentValue);
				}else if('-' == this.pendingOp){
					this.currents -= parseFloat(currentValue);
				}else if('*' == this.pendingOp){
					this.currents *= parseFloat(currentValue);
				}else if('/' == this.pendingOp){
					this.currents /= parseFloat(currentValue);
				}else{
					this.currents = parseFloat(this.getDisplayValue());
				}
				this.setDisplayValue(this.currents);
				this.pendingOp = value;
			}
		},
		
		clearAction: function(){
			this.currents = 0;
			this.pendingOp = '';
			this.setDisplayValue('0');
			this.flagNewNum = true;
		},
		
		cleanEntryAction: function(){
			this.setDisplayValue('0');
			this.flagNewNum = true;
		},
		
		negativeAction: function(){
			this.setDisplayValue(parseFloat(this.getDisplayValue()) * -1);
		},
		
		percentAction: function(){
			this.setDisplayValue((parseFloat(this.getDisplayValue())/100)*parseFloat(this.currents));
		},
		
		decimalAction: function(){
			var currentValue =  this.getDisplayValue();
			if(this.flagNewNum){
				currentValue = '0.';
				this.flagNewNum  = false;
			}else{
				if(currentValue.indexOf('.') == -1){
					currentValue += '.';
				}
			}
			this.setDisplayValue(currentValue);
		},
		
		//sets value to calculator display
		setDisplayValue: function(value){
			if(Ext.isDefined(value) && value != null){
				this.display.setValue(value);
			}
		},
		
		//gets value from calculator display
		getDisplayValue: function(){
			return this.display.getValue();
		}
	});	
};